package main

import (
	"fmt"
	"reflect"
)

type name string    // 新类型
type name1 = string // 别名

func main() {
	chapter_9_1()
	chapter_9_2()
	chapter_9_3_2()
}

//==============================================

func chapter_9_1() {
	fmt.Println("==========9.1")

	var a name
	var b name1
	fmt.Println(reflect.TypeOf(a), reflect.TypeOf(b))
}

//==============================================

type TypeA struct {
	Name string
	age  int8
}

func (obj TypeA) Print() {
	fmt.Println(obj.Name)
}

func chapter_9_2() {
	fmt.Println("==========9.2")

	var a TypeA
	a.Name = "000"

	var b = TypeA{Name: "123", age: 1}

	c := TypeA{"234", 2}

	d := &TypeA{Name: "345", age: 3}

	fmt.Println(a.Name, b.Name, c.Name, d.Name)
	a.Print()
}

//==============================================

type Lock interface {
	Lock() uint16
	Unlock(id uint16)
}

type interLock struct {
	id uint16
}

func (i *interLock) Lock() uint16 {
	i.id++
	fmt.Println("已上锁")
	return i.id
}

func (i *interLock) Unlock(id uint16) {
	if i.id == id {
		fmt.Println("解锁成功")
	} else {
		fmt.Println("解锁失败")
	}
}

type custLock struct{}

func (c *custLock) Lock() uint16 {
	//TODO implement me
	panic("implement me")
}

func chapter_9_3_2() {
	fmt.Println("==========9.3.2")

	var lock Lock
	lock = &interLock{}
	lockId := lock.Lock()
	lock.Unlock(lockId)

	//lock = &custLock{}
}
